package com.dogkr.server.service;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dogkr.common.FileDirEnum;
import com.dogkr.common.FileUtils;
import com.dogkr.server.entity.HostPhoto;
import com.dogkr.server.entity.HostPhotoAlbum;
import com.dogkr.server.mapper.HostPhotoAlbumMapper;
import com.dogkr.server.mapper.HostPhotoMapper;
import com.sean.common.service.BaseService;

@Service
public class PhotoService extends BaseService  {
	
	@Autowired
	private HostPhotoMapper hostPhotoMapper;
	@Autowired
	private HostPhotoAlbumMapper hostPhotoAlbumMapper;
	
	public List<HostPhoto> selectPhotoByHostKey(Long hostId) {
		return hostPhotoMapper.selectPhoto(this.getParamMap("hostId", hostId));
	}
	
	public List<HostPhoto> selectPhotoByAlbumKey(Long albumId) {
		return hostPhotoMapper.selectPhoto(this.getParamMap("albumId", albumId));
	}

	public HostPhoto selectByPrimaryKey(Long id) {
		return hostPhotoMapper.selectByPrimaryKey(id);
	}
	
	public List<HostPhotoAlbum> selectPhotoAlbumByHostKey(Long hostId) {
		return hostPhotoAlbumMapper.selectPhotoAlbum(this.getParamMap("hostId", hostId));
	}

	public int insertPhoto(HostPhoto record) throws IOException {
		FileUtils.removeUploadFile(record.getPhotoPath(), FileDirEnum.PHOTO);
		record.setCreateDate(new Date());
		record.setLastModifydate(record.getCreateDate());
		return hostPhotoMapper.insert(record);
	}
	
	public int updatePhoto(HostPhoto record) {
		record.setLastModifydate(new Date());
		return hostPhotoMapper.updateByPrimaryKeySelective(record);
	}
	
	public int deletePhoto(Long id, boolean updateAlbum) {
		HostPhoto record = hostPhotoMapper.selectByPrimaryKey(id);
		if(record.getAlbumId() != null && updateAlbum) {
			HostPhotoAlbum album = hostPhotoAlbumMapper.selectByPrimaryKey(record.getAlbumId());
			if(record.getPhotoPath().equals(album.getAlbumCoverPath())) {
				hostPhotoAlbumMapper.updateCoverPath(album.getId());
			}
		}
		try {
			FileUtils.deleteUploadFile(record.getPhotoPath(), FileDirEnum.PHOTO);
		} catch (Exception e) {
			log.error("照片文件删除失败.filename:" + record.getPhotoDescr(), e);
		}
		return hostPhotoMapper.deleteByPrimaryKey(id);
	}
	
	public int insertPhotoAlbum(HostPhotoAlbum record) {
		record.setCreateDate(new Date());
		record.setLastModifydate(record.getCreateDate());
		return hostPhotoAlbumMapper.insert(record);
	}
	
	public int updatePhotoAlbum(HostPhotoAlbum record) {
		record.setLastModifydate(record.getCreateDate());
		return hostPhotoAlbumMapper.updateByPrimaryKeySelective(record);
	}
	
	public int deletePhotoAlbum(Long id) {
		List<HostPhoto> photoList = hostPhotoMapper.selectPhoto(this.getParamMap("albumId", id));
		for (HostPhoto hostPhoto : photoList) {
			this.deletePhoto(hostPhoto.getId(), false);
		}
		return hostPhotoAlbumMapper.deleteByPrimaryKey(id);
	}

	public HostPhotoAlbum selectPhotoAlbumByKey(Long id) {
		return hostPhotoAlbumMapper.selectByPrimaryKey(id);
	}

	/**
	 * 设置相册封面
	 * @param newCoverId
	 * @return
	 */
	public void updatePhotoAlbumCover(Long newCoverId) {
		HostPhoto photo = hostPhotoMapper.selectByPrimaryKey(newCoverId);
		HostPhotoAlbum album = new HostPhotoAlbum();
		album.setId(photo.getAlbumId());
		album.setAlbumCoverPath(photo.getPhotoPath());
		hostPhotoAlbumMapper.updateByPrimaryKeySelective(album);
	}
}
